﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Xceed.Wpf.Toolkit.PropertyGrid;

namespace CPPEI.Coolzon.UI.XProperty.Editors
{
    /// <summary>
    /// XDataItemFilterEditor.xaml 的交互逻辑
    /// </summary>
    public partial class XDataItemFilterEditor : UI.XProperty.XPropertyValueEditor
    {
        public static DependencyProperty PropertyValueProperty = DependencyProperty.Register("PropertyValue", typeof(DataItemFilter), typeof(XDataItemFilterEditor), new PropertyMetadata(propertyChangedCallback));
        private static void propertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            XDataItemFilterEditor editor = d as XDataItemFilterEditor;
            if (e.NewValue != null)
            {
                editor.XDataFilter = (DataItemFilter)e.NewValue;
            }
            else
            {
                editor.XDataFilter = null;
            }
        }
        public DataItemFilter XDataFilter;
        public DataPart dataPart;
        public XDataItemFilterEditor()
        {
            InitializeComponent();
        }

        protected override DependencyProperty GetPropertyValue()
        {
            return PropertyValueProperty;
        }

        protected override void OnXInit(PropertyItem propertyItem)
        {
            Binding binding = new Binding();
            binding.Source = propertyItem;
            binding.Path = new PropertyPath("Value");
            binding.Mode = BindingMode.TwoWay;
            this.SetBinding(PropertyValueProperty, binding);
            dataPart = propertyItem.Instance as DataPart;
        }

        protected override void OnXInit(Part partInstance, XPropertyAttribute xProperty, PropertyInfo propertyInfo, object value)
        {
            Binding binding = new Binding();
            binding.Source = partInstance;
            binding.Path = new PropertyPath(propertyInfo.Name);
            this.SetBinding(PropertyValueProperty, binding);
            dataPart = (partInstance as DataPart);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            List<string> dataList = new List<string>();
            if (dataPart.OriginalDataResult != null && dataPart.OriginalDataResult.DataSeries != null && dataPart.OriginalDataResult.DataSeries.Length > 0 && dataPart.OriginalDataResult.DataSeries[0].DataItems != null && dataPart.OriginalDataResult.DataSeries[0].DataItems.Length > 0)
            {
                foreach (var item in dataPart.OriginalDataResult.DataSeries[0].DataItems)
                {
                    dataList.Add(item.Id);
                }
            }
            DataFilterEditWindow window = new DataFilterEditWindow(XDataFilter, dataList);
            if (window.ShowDialog().Value)
            {
                XDataFilter = new DataItemFilter();
                foreach (var item in window.RetDataFilter)
                {
                    XDataFilter.Add(item);
                }
                this.UpdateXPropertyValue(XDataFilter);
            }
        }
    }
}